'use strict';
const qs = require('qs');
const mockjs = require('mockjs');
let meetingData = {};
if (!global.meetingData) {
  const data1 = mockjs.mock({
    'data|10': [{
      'key|+1': 1,
      'time|1': [
        '2017-01-01',
        '2016-11-20'
      ],
      'title': '@cword(4, 20)',
      'type|1': ['全员大会', '部门例会'],
      'status|1': [
        '未召开',
        '已召开',
      ],
      'uid|1': [11],
      'depart|1': ['2','3',],
      'content': 'sslsllslsls'
    }],
    page: {
      total: 1,
      current: 1
    }
  });
  meetingData = data1;
  global.meetingData = meetingData;
}else{
  meetingData = global.meetingData;
}
module.exports = {
  'GET /api/meeting' (req, res) {
    const params = qs.parse(req.query);  
    var querydata;
    if (params) {
      querydata = meetingData.data.filter(function(data) {
        if (params.role) {
          if (params.role == '部门例会') {
            return data.role == params.role && data.depart == params.depart;
          }else {
            return data.role == params.role;
          }
        }else{
          return data.depart == params.depart;
        }
      });
    }else{
      querydata = meetingData.data;
    }
    console.log('querydata',querydata);
    res.json({
      success: 'true',
      data: {
        conditions: params,
        data: querydata
      }
    });
  },

  'POST /api/meeting' (req,res) {
    const newData = qs.parse(req.body);
    setTimeout(function () {
      res.json({
        success: 'true',
      })
    }, 500);
  },

  'PUT /api/meeting' (req,res) {
    const newData = qs.parse(req.body);
    console.log(req.body);
    meetingData.data.push(newData);
    // console.log(meetingData.data);
    setTimeout(function () {
      res.json({
        success: 'true'
      })
    },500);
  }
};
